
package com.kanjian.music.adapter;

import android.os.Bundle;
import android.support.v4.app.LoaderManager.LoaderCallbacks;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

import com.kanjian.music.R;
import com.kanjian.music.activity.BaseActivity;
import com.kanjian.music.api.ApiRequestManager;
import com.kanjian.music.constants.IntentConstants;
import com.kanjian.music.entity.FM;
import com.kanjian.music.fragment.FMFragment;

/**
 * 推荐页ListAdapter
 * @author xuyanjun@kanjian.com
 */
public class FMListAdapter extends MusicSubFragmentListAdapater<FM> {
    
    private BaseActivity mActivity;
    
    private FMFragment mFragment;
    
    public FMListAdapter(BaseActivity context, FMFragment fragment) {
        super(context);
        this.mActivity = context;
        this.mFragment = fragment;
    }
    
    public void registerOnItemClickListener(ListView listView) {
        listView.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                FM fm = getItem(position);
                mFragment.mClickRadioType = fm.mFMId;
                Bundle args = new Bundle();
                args.putSerializable(IntentConstants.HTTPLOADERREQUESTPARAM, ApiRequestManager.getRadioRequest(fm.mFMId));
                mActivity.getSupportLoaderManager().restartLoader(0, args, mFragment);
            }
        });        
    }

    @Override
    public int getItemViewType(int position) {
        if (position == 0) {
            return VIEW_TYPE_FAKE;
        } else {
            return VIEW_TYPE_DATA;
        }
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        super.getView(position, convertView, parent);
        if (getItemViewType(position) == VIEW_TYPE_FAKE) {
            return mListFakeView;
        } else {
            ViewHolder holder;
            if (convertView == null || convertView.getTag() == null) {
                holder = new ViewHolder();
                convertView = mLayoutInflater.inflate(R.layout.item_list_fm, null);
                holder.mTv = (TextView) convertView.findViewById(R.id.item_list_fm_title);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }
            FM fm = getItem(position);
            holder.mTv.setText(fm.mName);
            return convertView;
        }
    }

    static class ViewHolder {
        TextView mTv;
    }

}
